package vashion.azeroth.back.controller.tran;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vashion.azeroth.back.core.context.BackUserContext;
import vashion.azeroth.contant.CommonErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.tran.pojo.PersonalDisburse;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMemberService;
import vashion.azeroth.core.service.tran.PersonalDisburseService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台用户登陆页面
 */
@Controller
@RequestMapping("/back/login")
public class BackLoginController {

    @Autowired
    private PersonalDisburseService<PersonalDisburse> disburseService;
    @Autowired
    private PersonalMemberService<PersonalMember> personalMemberService;
    @Autowired
    protected BackUserContext userContext;

    /**
     * 登陆前页面
     *
     * @param admin
     * @param request
     * @return
     */
    @RequestMapping("index.htm")
    public String loginPage(Model model, @ModelAttribute("member") PersonalMember admin, HttpServletRequest request) {
        return "/main/loginPage";
    }

    /**
     * 登陆
     *
     * @param admin
     * @param request
     * @return
     */
    @RequestMapping("login.htm")
    public String login(Model model, @ModelAttribute("member") PersonalMember admin, HttpServletRequest request) {
    	
    	String errorMsg = "";
    	if (null == userContext.getLoginUser(request)) {
    		if (StringUtils.isBlank(admin.getMemberName())) {
    			errorMsg += "用户名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage() + "。";
    		}
    		if (StringUtils.isBlank(admin.getMemberPwd())) {
    			errorMsg += "密码" + GenericErrorEnum.NOT_EMPTY.getErrorMessage() + "。";
    		}
    		if (!StringUtils.isBlank(errorMsg)) {
    			model.addAttribute("errorMsg", errorMsg);
            	model.addAttribute("memberName", admin.getMemberName());
    			return "/main/loginPage";
    		}
    		admin.setMemberName(admin.getMemberName().trim());
            ApiResult<PersonalMember> apiResult = personalMemberService.backLogin(admin);
            if (apiResult.isSuccess()) {
                userContext.setLoginUser(apiResult.getData(), request);
            } else {
            	errorMsg = "用户名或者密码错误！";
            	model.addAttribute("errorMsg", errorMsg);
            	model.addAttribute("memberName", admin.getMemberName());
    			return "/main/loginPage";
            }
    	}
    	model.addAttribute("backUser", userContext.getLoginUser(request));
        return "/main/main";
    }

    /**
     * 获取登陆信息
     *
     * @return
     */
    @RequestMapping("getUserInfo.htm")
    @ResponseBody
    public ApiResult<PersonalMember> getUserInfo(HttpServletRequest request) {

        PersonalMember member = userContext.getLoginUser(request);

        return ApiResult.newSuccessResult(member);
    }

    /**
     * pc注册接口
     *
     * @return
     */
    @RequestMapping("pcRegister.htm")
    @ResponseBody
    public ApiResult<PersonalMember> pcRegister(@ModelAttribute("member") PersonalMember member) {

        return personalMemberService.backRegister(member);
    }

    /**
     * 删除管理员
     *
     * @return
     */
    @RequestMapping("deleteAdmin.htm")
    @ResponseBody
    public ApiResult<PersonalMember> deleteAdmin(@ModelAttribute("member") PersonalMember member) {

        //FIXME 还没有实现
        return ApiResult.newErrorResult(CommonErrorEnum.SYSTEM_ERROR.getErrorCode(), CommonErrorEnum.SYSTEM_ERROR.getErrorMessage());
    }

    /**
     * 获取pc管理员列表
     *
     * @return
     */
    @RequestMapping("getAdminList.htm")
    @ResponseBody
    public ApiResult<List<PersonalMember>> getAdminList() {

        return personalMemberService.getAdminList();
    }


    @RequestMapping("logout.htm")
    public String logout(HttpServletRequest request) {
        userContext.removeLoginUser(request);
        return "/main/loginPage";
    }

}
